﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DataAccessLayer;
using DataTransferObject;

namespace BusinessLogicLayer
{
    public class HoSoBenhNhanBLL : BLLBase
    {
        private readonly Ibn_HoSoBenhNhanDAL _dal;
        public HoSoBenhNhanDto create = new HoSoBenhNhanDto();

        public HoSoBenhNhanBLL(string connectionString = "")
            : base(connectionString)
        {
            this._dal = new bn_HoSoBenhNhanDAL(this.DatabaseFactory);
        }

        public IQueryable<HoSoBenhNhanDto> Get(int facilityId, string key = "")
        {
            var query = this._dal.GetAll()
                .Where(e => e.Id == facilityId)
                .Select(e => new HoSoBenhNhanDto
                    {
                        Id = e.Id,
                        Ten = e.Ten,
                        DiaChi = e.DiaChi,
                        DienThoai = e.DienThoai,
                        Email = e.Email,
                        GioiTinh = e.GioiTinh,
                        Ho = e.Ho,
                        IsDelete = e.IsDelete,
                        IsFinish = e.IsFinish,
                        MaThe = e.MaThe,
                        NamSinh = e.NamSinh,
                    });

            if (!string.IsNullOrEmpty(key))
            {
                query = query.Where(e => e.Ten.StartsWith(key));
            }
            return query;
        }
        public List<HoSoBenhNhanDto> GetAll()
        {
            var query = this._dal.GetAll()
                .Select(e => new HoSoBenhNhanDto
                {
                    Id = e.Id,
                    Ten = e.Ten,
                    DiaChi = e.DiaChi,
                    DienThoai = e.DienThoai,
                    Email = e.Email,
                    GioiTinh = e.GioiTinh,
                    Ho = e.Ho,
                    MaThe = e.MaThe,
                    NamSinh = e.NamSinh,
                });
            return query.ToList();
        }
        public IQueryable<HoSoBenhNhanDto> Query()
        {
            var query = this._dal.GetAll()
                .Select(e => new HoSoBenhNhanDto
                {
                    Ten = e.Ten,
                    DiaChi = e.DiaChi,
                    DienThoai = e.DienThoai,
                    Email = e.Email,
                    GioiTinh = e.GioiTinh,
                    Ho = e.Ho,
                    Id = e.Id,
                    IsDelete = e.IsDelete,
                    IsFinish = e.IsFinish,
                    MaThe = e.MaThe,
                    NamSinh = e.NamSinh,
                });
            return query;
        }
        public void Update(HoSoBenhNhanDto dto, int facilityId)
        {
            var entity = this._dal.GetAll().FirstOrDefault(e => e.Id == dto.Id && e.Id == facilityId);
            if (entity == null) throw new Exception("Đối tượng không tồn tại");
            entity.Ten = dto.Ten;
            entity.DiaChi = dto.DiaChi;
            entity.DienThoai = dto.DienThoai;
            entity.Email = dto.Email;
            entity.GioiTinh = dto.GioiTinh;
            entity.Ho = dto.Ho;
            entity.Id = dto.Id;
            entity.IsDelete = dto.IsDelete;
            entity.IsFinish = dto.IsFinish;
            entity.MaThe = dto.MaThe;
            entity.NamSinh = dto.NamSinh;
            this.SaveChanges();
        }
        public void Insert(HoSoBenhNhanDto dto)
        {
            var entity = new bn_HoSoBenhNhan();
            entity.Ten = dto.Ten;
            entity.DiaChi = dto.DiaChi;
            entity.DienThoai = dto.DienThoai;
            entity.Email = dto.Email;
            entity.GioiTinh = dto.GioiTinh;
            entity.Ho = dto.Ho;
            entity.Id = dto.Id;
            entity.IsDelete = dto.IsDelete;
            entity.IsFinish = dto.IsFinish;
            entity.MaThe = dto.MaThe;
            entity.NamSinh = dto.NamSinh;
            this._dal.Add(entity);
            this.SaveChanges();
            dto.Id = entity.Id;
        }
        public void Delete(int id)
        {
            new BenhBanDau_NoiDungDieuTriBLL().DeleteWhereHSBenhNhan(id);
            new ThongTinXacDinhBenhBanDauBLL().DeleteWhereHSBenhNhan(id);
            new ToaThuocBLL().DeleteWhereHSBenhNhan(id);
            new ToaDieuTriCongCuBLL().DeleteWhereHSBenhNhan(id);
            new TrieuChungBanDauBLL().DeleteWhereHSBenhNhan(id);
            var entity = this._dal.GetAll().FirstOrDefault(e => e.Id == id);
            if (entity == null) throw new Exception("Đối tượng không tồn tại");
            this._dal.Delete(entity);
            this.SaveChanges();
        }
        public IEnumerable<ThongTinThuocDto> GetThongTinThuoc(int Id)
        {
            var query = new ToaThuocBLL().GetAll().Where(e => e.IdBenhNhan == Id).
                Join(new ToaThuocChiTietBenhNhanBLL().GetAll(), y => y.Id, c => c.IdToaThuoc,
                    (y, c) => new { c.IdThuoc, c.SL, c.GhiChu }).
                    Join(new DmThuocBLL().GetAll(), c => c.IdThuoc, d => d.Id,
                    (c, d) => new ThongTinThuocDto { MaThuoc = d.MaThuoc, TenThuoc = d.TenThuoc, Amount = d.SLThuoc, GhiChu = c.GhiChu });
            return query;
        }
        public IEnumerable<DieuTriDto> GetDieuTri(int Id)
        {
            var query = new ToaDieuTriCongCuBLL().GetAll().Where(e => e.IdBenhNhan == Id).
                Join(new CongCuDieuTriBLL().GetAll(), x => x.IdCongCuDieuTri, y => y.Id, (x, y) => new DieuTriDto() { Amount = x.Amount, Id =x.Id, Ten = y.Ten });
            return query;
        }
    }
}
